import { System } from "csharp";
import LoadUtils from "./LoadUtils";
import UrlUtils from "./UrlUtils";

/**
 * 本地缓存
 * @author clong 2020.11.28
 */
export default class LocalCookie {

    private static inst: LocalCookie = null;
    public static get Inst(): LocalCookie {
        return this.inst = this.inst || new LocalCookie();
    }

    public static readonly COOKIE_NAME:string = "cache";//本地缓存名字

    /**项目名字 **/
    public static readonly PROJECT_NAME:string = "FairyPluInProject";

    /**缓存字典 */
    private cacheMap:Object = {};

    // private so: any;//

    public init( cacheName:string = LocalCookie.COOKIE_NAME , hanlder:Laya.Handler = null ):void{

        let url = this.getCacheUrl( cacheName );
        let so = this.cacheMap[ cacheName ];
        if( so == null ){            
            LoadUtils.loadText( url , Laya.Handler.create( this , (txt:string)=>{
                try{
                    if(!txt){
                        this.save();
                    }else{
                        so = JSON.parse( txt );
                    }
                }catch(e){
                    console.error("获取缓存数据失败! error: " + e);
                    so = {};
                }     
                this.cacheMap[ cacheName ] = so;
                console.log("获取缓存数据 so: " + JSON.stringify(so));
                hanlder && hanlder.runWith( so );
            }));
        }else{
            hanlder && hanlder.runWith( so );
        }
    }

    private getCacheUrl( name:string = LocalCookie.COOKIE_NAME ):string{

        return UrlUtils.Cache +  name + ".json";
    }

    /**
     * 保存缓存数据
     */
    public save( cookieName:string = LocalCookie.COOKIE_NAME ):void{

        let url = this.getCacheUrl( cookieName );
        if( !System.IO.File.Exists(url) ){
            System.IO.File.Create( url ); 
            console.log("创建文件: " + url);                              
        }
        let so = this.cacheMap[ cookieName ] || {};
        this.cacheMap[ cookieName ] = so;
        let json = JSON.stringify( so );
        try {
            System.IO.File.WriteAllText( url , json ); 
            console.log("------>写入文件成功 url: " + url + " json: " + json);
        } catch (error) {
            console.log("------>写入文件失败 url: " + url + " json: " + json);  
            console.error( error );
        }
        
    }

    /**
     * 写入缓存
     * @param	key
     * @param	value
     * @param	propertyName
     */
    public write( key:string, value:string, cookieName:string = LocalCookie.COOKIE_NAME): void {

        try {
            // so = so || SharedObject.getLocal(cookieName, "/");
            // so.data[propertyName] = value;
            // so.flush();

            // FairyEditor.App.localStore.Set( key , value );

            let so = this.cacheMap[ cookieName ] || {};
            so[ key ] = value;
            this.cacheMap[ cookieName ] = so;
            this.save( cookieName );

            console.log( "写入本地缓存 key: " + key + " value: " + value );
        } catch (e) {
            console.log( e );
        }
    }

    /**
     * 读取缓存
     * @param	cookieName
     * @param	key
     * @return
     */
    public read(key:string, cookieName:string = LocalCookie.COOKIE_NAME):any{

        try {

            // so = so || SharedObject.getLocal(cookieName, "/");
            // return so.data[propertyName];

            // let data = FairyEditor.App.localStore.Get( key );
            // console.log( "-------->读取本地缓存 key: " + key + " value: " + data );
            // return data;
            
            let so = this.cacheMap[ cookieName ];
            // so && console.log( "-------->读取本地缓存 so: " + JSON.stringify(so) + " key: " + key );
            return so ? so[ key ] : null;

        } catch (e) {
            console.log( "-------->读取本地缓存 key: " + key + " 错误!!! e: " + e);
            // ObjectUtils.showObject( FairyEditor.App.localStore );
            console.log( "-----------------------------------------------> ");
        }

        return null;
    }

    /**
     * 清除缓存
     * @param	cookieName
     */
    public clear(cookieName:string = LocalCookie.COOKIE_NAME): void {

        try {

            // so = so || SharedObject.getLocal(cookieName, "/");

            // so.clear();

            // FairyEditor.App.localStore.Clear();

        } catch (e) {

        }
    }

    /**
     * 释放所有缓存
     * @param	cookieName
     */
    public dispose(cookieName:string = LocalCookie.COOKIE_NAME): void {

        try {

            // so = so || SharedObject.getLocal(cookieName, "/");
            // so.clear();

        } catch (e) {

        }
    }
}